package ua.org.enginer.controls.map {
	
	import com.yahoo.maps.api.YahooMap;
	import com.yahoo.maps.api.YahooMapEvent;
	import com.yahoo.maps.api.core.location.Address;
	import com.yahoo.maps.api.core.location.LatLon;
	import com.yahoo.maps.api.intl.MapLocales;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flexlib.controls.PromptingTextInput;
	
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.controls.LinkButton;
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.FlexMouseEvent;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;
	
	[Event(name="close", type="mx.events.CloseEvent")]
	[Event(name="selected", type="ua.org.enginer.controls.map.MapEvent")]
	
	public class YahooMapSelector extends Canvas {
		
		public var center:LatLon = new LatLon(50.44194900187433, 30.536298042968752);
		
		protected var mapHolder:UIComponent
		protected var addressBar:HBox
		//public var address:String
		public var map:YahooMap;
		protected var addressInput:PromptingTextInput
		
		public function YahooMapSelector() {
			super();
			addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, onClickOutside)
			height = Application.application.stage.height - 100
			width = Application.application.stage.width - 100
		}
		
		//private var _address:String;
		public function set address(value:String):void {
			//_address = value;
			addressInput.text = value
		}

		public function get address():String {
			return addressInput.text;//_address;
		}

		protected function onClickOutside(event:Event):void {
			close()
		}
		
		protected function close():void {
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE))
		}
		
		override protected function createChildren():void {
			super.createChildren()
			
			mapHolder = new UIComponent()
			addChild(mapHolder)
			createMap()
			mapHolder.addChild(map)
			
			addressBar = new HBox()
			addressBar.styleName = getStyle("addressBar");
			addChild(addressBar)
			
			addressInput = new PromptingTextInput()
			addressInput.styleName = getStyle("addressInput");
			addressInput.prompt = "адрес"
			addressInput.addEventListener(FlexEvent.ENTER, onAddressButtonClick)
			addressBar.addChild(addressInput)
			
			var addressButton:LinkButton = new LinkButton()
			addressButton.styleName = getStyle("addressButton");
			addressButton.label = "найти"
			addressButton.addEventListener(MouseEvent.CLICK, onAddressButtonClick)
			addressBar.addChild(addressButton)
			
			var closeButton:LinkButton = new LinkButton()
			closeButton.styleName = getStyle("closeButton");
			closeButton.label = "выбрать"
			closeButton.addEventListener(MouseEvent.CLICK, onSelected)
			addChild(closeButton)
		}
		
		protected function onSelected(event:Event):void {
			
			dispatchEvent(new MapEvent(MapEvent.SELECTED, false, false, 
				new MapPoint(coordinates.lat, coordinates.lon, address)))
			close()
		}
		
		private function onAddressButtonClick(event:Event):void {
			if (addressInput.text) findByAddress(addressInput.text)
		}
		
		public function findByAddress(address:String):void {
			addressInput.text = address
			trace(address);
			if (!address) return;
			map.setCenterAddress(new Address(address))
		}
		
		public function get coordinates():LatLon {
			return map.centerLatLon ? map.centerLatLon : center
		}
		
		public function set coordinates(value:LatLon):void {
			if (map.initialized) map.centerLatLon = value
			else center = value
		}
		
		private function createMap():void {
			var appid:String = "7N14gmnV34Eufm61fSYuihVQL3kYn65z6_BcvjmVn4igOmGgOiAVU1X.QLxJkoZ5FA--"
			
			map = new YahooMap()
			map.addEventListener(YahooMapEvent.MAP_INITIALIZE, handleMapInitialize); 
			map.init(appid, width, height, MapLocales.EN)
			
			map.addCrosshair()
			map.addPanControl()
			map.addTypeWidget()
			map.addZoomWidget()
		}

		private function handleMapInitialize(event:YahooMapEvent):void {
			// listen for UIComponent resize event
			mapHolder.addEventListener(ResizeEvent.RESIZE, handleContainerResize);

			// set zoom level and center LatLon
			map.zoomLevel=8;
			// kiev
			map.centerLatLon = center
		}

		private function handleContainerResize(event:ResizeEvent):void {
			// resize the map based on the containers height and width.
			map.setSize(mapHolder.width, mapHolder.height);
		}
		
		public static function show():YahooMapSelector {
			var popUpWindow:YahooMapSelector = new YahooMapSelector()
			
			PopUpManager.addPopUp(popUpWindow, Application.application as DisplayObject, true)
			PopUpManager.centerPopUp(popUpWindow)
			
			popUpWindow.addEventListener(CloseEvent.CLOSE, function():void {
				PopUpManager.removePopUp(popUpWindow)
				trace(popUpWindow.coordinates)
			})
			
			return popUpWindow
		}
	}
}

